package com.assistant.admin.service.impl;

import com.assistant.admin.mapper.SpManagerMapper;
import com.assistant.admin.pojo.Login;
import com.assistant.admin.pojo.PageResult;
import com.assistant.admin.pojo.SpManager;
import com.assistant.admin.service.SpManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class SpManagerServiceImpl implements SpManagerService {

    @Autowired
    private SpManagerMapper spManagerMapper;

    @Override
    public int insertSpManager(SpManager spManager) {
        Example example = new Example(SpManager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",spManager.getUsername());
        List<SpManager> spManagers = spManagerMapper.selectByExample(example);
        if(!spManagers.isEmpty()){
            return -1;
        }
        spManager.setRegister_time(String.valueOf(new Date().getTime()/1000));
        return spManagerMapper.insertSelective(spManager);
    }

    @Override
    public int deleteSpManager(Integer id) {
        return spManagerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        SpManager spManager = new SpManager();
        spManager.setId(id);
        spManager.setMg_state(status);
        return spManagerMapper.updateByPrimaryKeySelective(spManager);
    }

    @Override
    public List<SpManager> isCheckLogin(Login login) {
        Example example = new Example(SpManager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",login.getUsername()).andEqualTo("password", login.getPassword());
        System.out.println(login.getUsername());
        return spManagerMapper.selectByExample(example);
    }

    @Override
    public int updatePassword(Integer id,SpManager spManager) {
        spManager.setId(id);
        return spManagerMapper.updateByPrimaryKeySelective(spManager);
    }

    @Override
    public PageResult<SpManager> getListSpManager(Integer page, Integer rows, String key) {
        Example example = new Example(SpManager.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(key)){
            criteria.andLike("username","%"+key+"%").orEqualTo("username",key);
        }
        PageHelper.startPage(page,rows);
        List<SpManager> list = spManagerMapper.selectByExample(example);
        PageInfo<SpManager> brandPageInfo = new PageInfo<>(list);
        return new PageResult<SpManager>(brandPageInfo.getTotal(),brandPageInfo.getList());
    }

    @Override
    public int updateRootAuthority(Integer id, Integer rootAuthority) {
        SpManager spManager = new SpManager();
        spManager.setId(id);
        spManager.setRoot_authority(rootAuthority);
        return spManagerMapper.updateByPrimaryKeySelective(spManager);
    }

    @Override
    public SpManager selectSpManager(Integer id) {
        System.out.println("selectSpManager");
        return spManagerMapper.selectByPrimaryKey(id);
    }
}
